package cn.aspart.aspectj.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
@Order(1)
public class TimeAspect {

    @Pointcut("execution (* cn.aspart.aspectj.service.*.*(..))")
    public void timePointcut(){

    }


    @Around("timePointcut()")
    public void timeElapsed(ProceedingJoinPoint joinPoint){
        long start = System.currentTimeMillis();
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

        long stop = System.currentTimeMillis();
        log.info("time elapsed: {}s", ((stop - start) / 1000));
    }

}
